﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;

namespace JM.JCY.SharePointComponent.News.EditNews
{
    public partial class EditNewsUserControl : UserControl
    {
        public int _NewsId
        {
            set { ViewState["_NewsId"] = value; }
            get { return (int)ViewState["_NewsId"]; }
        }

        public string _category
        {
            set { ViewState["_category"] = value; }
            get { return (string)ViewState["_category"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["NewsId"] != null)
                {
                    _NewsId = Int32.Parse(Request.QueryString["NewsId"]);

                    using (NewsProxy newsProxy = new NewsProxy())
                    {
                        NewsDataSet.T_AnnouncementDataTable annouceDT = newsProxy.GetNewsById(_NewsId);
                        Session["announceDT"] = annouceDT;
                        this.txtSubject.Text = annouceDT[0].Subject;
                        this.txtSummary.Text = annouceDT[0].Summary;
                        this.imgPic.ImageUrl = "/"+annouceDT[0].ImageUrl;


                        _category = annouceDT[0].Category;
                        this.hfCategory.Value = _category;

                        if (_category == "News")
                        {
                  
                            this.lblDisplay.Text = "编辑新闻";
                        }
                        else
                        {
             
                            this.lblDisplay.Text = "编辑领导讲话";
                        }

                    }

                }
                if (Request.QueryString["category"] != null)
                {
                    _category = Request.QueryString["category"].ToString();
                    this.hfCategory.Value = _category;
                }

                if (_category == "News")
                {
                    this.lblDisplay.Text = "编辑新闻";
                }
                else
                {
                    this.lblDisplay.Text = "编辑领导讲话";
                }
            }

        }

        protected NewsDataSet.T_AnnouncementDataTable GetNewsDataFromPage(string status)
        {
            NewsDataSet.T_AnnouncementDataTable dt = new NewsDataSet.T_AnnouncementDataTable();
            if (Session["NewsData"] != null)
            {
                dt = (NewsDataSet.T_AnnouncementDataTable)Session["NewsData"];
            }
            else
                dt = (NewsDataSet.T_AnnouncementDataTable)Session["announceDT"];

            NewsDataSet.T_AnnouncementRow newsRow;
            newsRow = (NewsDataSet.T_AnnouncementRow)dt.Rows[0];

            newsRow.Subject = txtSubject.Text.Trim();
            newsRow.Summary = txtSummary.Text.Trim();

            if (this.filePicture.HasFile)
            {
                newsRow.ImageUrl = JCYSharePointHelper.UploadPublishedFile(this.filePicture, Guid.NewGuid().ToString(), "News");
                this.hfPicPath.Value = newsRow.ImageUrl;
            }

            newsRow.Status = status;
            Common.JCYUtility.UpdateCommonFields(newsRow, SPContext.Current.Web.CurrentUser.Name);

            return dt;
        }

        protected void imgPreview_Click(object sender, ImageClickEventArgs e)
        {
            NewsDataSet.T_AnnouncementDataTable dt = GetNewsDataFromPage("");
            Session["NewsData"] = dt;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "popup('/_layouts/SharePointComponent/PreviewNews.aspx');", true);
        }

        protected void imgSaveDraft_Click(object sender, ImageClickEventArgs e)
        {
            EditNews(JCYConstant.Status.Draft);
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }

        protected void imgSavePublish_Click(object sender, ImageClickEventArgs e)
        {
            EditNews(JCYConstant.Status.Publish);
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }

        private void EditNews(string status)
        {
            using (NewsProxy newsProxy = new NewsProxy())
            {
                if (Session["announceDT"] != null)
                {
                    NewsDataSet.T_AnnouncementDataTable dt = (NewsDataSet.T_AnnouncementDataTable)Session["announceDT"];
                    NewsDataSet.T_AnnouncementRow editRow = dt[0];

                    editRow.Subject = this.txtSubject.Text;
                    editRow.Summary = this.txtSummary.Text;
                    editRow.Status = status;

                    if (this.filePicture.HasFile)
                    {
                        editRow.ImageUrl = JCYSharePointHelper.UploadPublishedFile(this.filePicture, Guid.NewGuid().ToString(), "News");
                    }
                    else if (this.hfPicPath.Value != string.Empty)
                    {
                        editRow.ImageUrl = this.hfPicPath.Value;
                    }

                    Common.JCYUtility.UpdateCommonFields(editRow, SPContext.Current.Web.CurrentUser.Name);


                    newsProxy.UpdateNews(dt);
                }

            }
        }
    }
}
